package com.credithc.customers.dataservice.service.impl;

import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.base.constants.ProductTypeConstants;
import com.credithc.customers.dataservice.Thread.OldDataThreadPoolExecutor;
import com.credithc.customers.dataservice.constants.SyncTypeEnum;
import com.credithc.customers.dataservice.constants.TableNameConstants;
import com.credithc.customers.dataservice.mapper.DLR.DlrMapper;
import com.credithc.customers.dataservice.po.CustomerNoPo;
import com.credithc.customers.dataservice.po.SystemLendPo;
import com.credithc.customers.dataservice.po.SystemProductPo;
import com.credithc.customers.dataservice.po.SystemUserPo;
import com.credithc.customers.dataservice.ro.CustomerAccountInfoRo;
import com.credithc.customers.dataservice.ro.CustomerProductInfoRo;
import com.credithc.customers.dataservice.ro.ProductInfoRo;
import com.credithc.customers.dataservice.service.OldDlrDataService;
import com.credithc.customers.dataservice.service.SyncDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;

@Service
@Slf4j
public class OldDlrDataServiceImpl implements OldDlrDataService {
    @Autowired
    DlrMapper dlrMapper;
    @Autowired
    SyncDataService syncDataService;
    @Autowired
    private OldDataThreadPoolExecutor oldDataThreadPoolExecutor;
    @Override
    public void oldProduct() {
        boolean f =true;
        Long num = dlrMapper.queryProductMaxId();
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+1001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.execute(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-1000);
                    }
                    List<SystemProductPo> systemUserPos = dlrMapper.queryDlrProduct(Long.toString(vernier.get()));
                    if (systemUserPos.size() > 0) {
                        bulidDlrProduct(systemUserPos);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    private void bulidDlrProduct(List<SystemProductPo> systemProductPos) {
        List<ProductInfoRo> productInfoRos = new ArrayList<>();
        for (SystemProductPo d: systemProductPos) {
            ProductInfoRo productInfoRo = new ProductInfoRo();
            productInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            productInfoRo.setDatabaseName(TableNameConstants.DLR_DATABASE);
            productInfoRo.setTableName(TableNameConstants.DLR_PRODUCT_QM_PRODUCT_MANAGER);
            productInfoRo.setProductChannel("DLR");
            productInfoRo.setSubSystemSign("DLR");
            productInfoRo.setSystemSign("DLR");
            productInfoRo.setProductCreateTime(d.getCreatTime());
            productInfoRo.setProductUpdateTime(d.getUpdateTime());
            productInfoRo.setProductType(Integer.toString(ProductTypeConstants.P2P_LEND));
            productInfoRo.setProductNo(d.getId());
            productInfoRo.setTableKey(d.getIncrementId()+"");
            productInfoRo.setProductName(d.getProductName());
            productInfoRos.add(productInfoRo);
        }
        syncDataService.syncProduct(productInfoRos);
    }

    @Override
    public void oldUser() {
        boolean f =true;
        Long num = dlrMapper.queryUserMaxId();
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+1001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-1000);
                    }
                    List<SystemUserPo> systemUserPos = dlrMapper.queryDlrQmUser(Long.toString(vernier.get()));
                    if (systemUserPos.size() > 0) {
                        bulidDlrUser(systemUserPos);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    @Override
    public void oldBankCard() {
        boolean f =true;
        Long num = dlrMapper.queryUserBankMaxId();
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+1001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-1000);
                    }
                    List<SystemUserPo> systemUserPos = dlrMapper.queryDlrQmBankCard(Long.toString(vernier.get()));
                    if (systemUserPos.size() > 0) {
                        bulidDlrBank(systemUserPos);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }


    }

    private void bulidDlrBank(List<SystemUserPo> dlrQmUserRos) {
        List<CustomerAccountInfoRo> customerAccountInfoRos = new ArrayList<>();
        for (SystemUserPo u:dlrQmUserRos) {
            CustomerAccountInfoRo customerAccountInfoRo = new CustomerAccountInfoRo();
            customerAccountInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerAccountInfoRo.setSubSystemSign("DLR");
            customerAccountInfoRo.setSystemSign("DLR");
            customerAccountInfoRo.setAccountCreateTime(u.getCreatTime());
            customerAccountInfoRo.setAccountUpdateTime(u.getUpdateTime());
            customerAccountInfoRo.setUserSourceId(u.getUserId());
            customerAccountInfoRo.setAccountType(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_BANK_CARD);
            customerAccountInfoRo.setAccountNo(u.getCustomerId());
            customerAccountInfoRo.setCustomerNo(u.getCustomerNo());

            customerAccountInfoRo.setAccountStatus(StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())?CustomerConstants.STATUS_ACCOUNT_AVAILABLE:CustomerConstants.STATUS_ACCOUNT_UNAVAILABLE);
            if (!"A".equals(u.getSts())){
                customerAccountInfoRo.setAccountStatus(CustomerConstants.STATUS_ACCOUNT_UNAVAILABLE);
            }
            customerAccountInfoRos.add(customerAccountInfoRo);

        }
        syncDataService.syncCustomerAccount(customerAccountInfoRos);
    }

    private void bulidDlrUser(List<SystemUserPo> dlrQmUserRos) {
        List<CustomerAccountInfoRo> customerAccountInfoRos = new ArrayList<>();
        for (SystemUserPo u:dlrQmUserRos) {
            CustomerAccountInfoRo customerAccountInfoRo = new CustomerAccountInfoRo();
            customerAccountInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerAccountInfoRo.setSubSystemSign("DLR");
            customerAccountInfoRo.setSystemSign("DLR");
            customerAccountInfoRo.setAccountCreateTime(u.getCreatTime());
            customerAccountInfoRo.setAccountUpdateTime(u.getUpdateTime());
            customerAccountInfoRo.setAccountNo(u.getCustomerId());
            customerAccountInfoRo.setCustomerNo(u.getCustomerNo());
            customerAccountInfoRo.setAccountType(u.getUserType() ==Integer.parseInt(CustomerConstants.DLR_ACCOUNT_TYPE_LEND) ? CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LEND:CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LOAN);
            customerAccountInfoRo.setUserSourceId(u.getUserId());
            customerAccountInfoRo.setAccountStatus(StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())?CustomerConstants.STATUS_ACCOUNT_AVAILABLE:CustomerConstants.STATUS_ACCOUNT_UNAVAILABLE);
            customerAccountInfoRo.setCustomerRole(u.getUserType() ==Integer.parseInt(CustomerConstants.DLR_ACCOUNT_TYPE_LEND) ? CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LEND:CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LOAN);
            if (StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())) {
                customerAccountInfoRos.add(customerAccountInfoRo);
            }

        }
        syncDataService.syncCustomerAccount(customerAccountInfoRos);
    }

    @Override
    public void oldLend() {
        boolean f =true;
        Long num = dlrMapper.queryLendMaxId();
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+1001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-1000);
                    }
                    List<SystemLendPo> systemUserPos = dlrMapper.queryDlrLend(Long.toString(vernier.get()));
                    if (systemUserPos.size() > 0) {
                        bulidDlrLend(systemUserPos);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }

    }

    private void bulidDlrLend(List<SystemLendPo> dlrLendRos) {
        List<CustomerProductInfoRo> customerProductInfoRos = new ArrayList<>();
        for (SystemLendPo d:dlrLendRos){
            CustomerProductInfoRo customerProductInfoRo = new CustomerProductInfoRo();
            customerProductInfoRo.setDatabaseName(TableNameConstants.DLR_DATABASE);
            customerProductInfoRo.setTableName(TableNameConstants.DLR_CUSTOMER_PRODUCT_QM_LEND_ORDER);
            customerProductInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerProductInfoRo.setSubSystemSign("DLR");
            customerProductInfoRo.setSystemSign("DLR");
            customerProductInfoRo.setProductSourceSystem(CustomerConstants.DLR_CODE+"");
            customerProductInfoRo.setTableKey(d.getId());
            customerProductInfoRo.setProductAmount(d.getAmount());
            customerProductInfoRo.setProductNo(d.getProductId());
            customerProductInfoRo.setProductStartTime(d.getCapitalEffectDate());
            customerProductInfoRo.setProductEndTime(d.getExpireDate());
            customerProductInfoRo.setProductDeadline(d.getPeriod()+"");
            customerProductInfoRo.setProductStatus(CustomerProductInfoServiceImpl.getYZTStatus("DLR"+d.getSts()));
            customerProductInfoRo.setUserResourceId(d.getUserId());
            CustomerNoPo byUserId = dlrMapper.getByUserId(d.getUserId());
            if (byUserId !=null){
                customerProductInfoRo.setCustomerNo(byUserId.getCustomerNo());
                customerProductInfoRo.setPreCustomerNo(byUserId.getPreCustomerNo());
            }

            customerProductInfoRos.add(customerProductInfoRo);
        }
        syncDataService.syncCustomerProduct(customerProductInfoRos);
    }

    /*private String getCustomerRole(String customerNo,String systemSign, String subSystemSign){
        String roleSource = commonService.getRoleSource(systemSign, subSystemSign);
        String destRole = null;
        if(StringUtils.isNotBlank(roleSource)){
            if(!CustomerConstants.RELATION_SOURCE_LIST.contains(roleSource)){
                //表示角色是固定值
                destRole =  roleSource;
            } else if (CustomerConstants.ROLE_SOURCE_PRODUCT.equals(roleSource)){
                //TODO 表示要从产品购买记录分析出角色 destRole = ??
                destRole =  null;
            }

            if(StringUtils.isNotBlank(destRole)){
                CustomerRolePo customerRole = commonService.getCustomerRole(customerNo, destRole, systemSign, subSystemSign);
                if(customerRole == null){
                    return destRole;
                }
            }
        }
        return null;
    }*/
}
